﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Weazel.Badger;

namespace BadgerTest.FullAdder
{
	/// <summary>
	/// Summary description for FullAdderTest
	/// </summary>
	[TestClass]
	public class FullAdderTest
	{
		[TestMethod]
		public void TestFullAdder()
		{
			string workingDir = "c:\\badger_test_temp\\";
			string description = BadgerTestUtillities.GetResourceFile("FullAdder.fulladder.fdl");

			Weazel.Gezel.Model.Model model =
				BadgerTestUtillities.ParseAndVerify(description, 0);
			
			if (Directory.Exists(workingDir))
				Directory.Delete(workingDir, true);

			Directory.CreateDirectory(workingDir);

			Weazel.Badger.Configuration cfg = new Configuration();
			cfg.OutputFolder = "c:\\badger_test_temp\\";
			cfg.ConvertDisplayDirectives = true;
			cfg.CreateClockAndResetGenerationProcesses = true;		

			Weazel.Badger.ConversionContext conversion =
				new Weazel.Badger.ConversionContext(model, cfg);

			conversion.Convert();
						
			Assert.IsTrue(File.Exists(workingDir + "\\" + "S.vhd"));
			Assert.IsTrue(File.Exists(workingDir + "\\" + "adder.vhd"));
			Assert.IsTrue(File.Exists(workingDir + "\\" + "connect.vhd"));
			Assert.IsTrue(File.Exists(workingDir + "\\" + "fulladder.vhd"));
			Assert.IsTrue(File.Exists(workingDir + "\\" + "monitor.vhd"));
			Assert.IsTrue(File.Exists(workingDir + "\\" + "reg_add.vhd"));
			Assert.IsTrue(File.Exists(workingDir + "\\" + "stimuli.vhd"));
			Assert.IsTrue(File.Exists(workingDir + "\\" + "std_logic_arithext.vhd"));			
			Assert.IsTrue(File.Exists(workingDir + "\\" + "weazel_text_io.vhd"));

			// create "work" library using vlib
			BadgerTestUtillities.RunVLib(workingDir, "work");
			
			// compile vhdl
			BadgerTestUtillities.RunVCom(workingDir, "std_logic_arithext.vhd");
			BadgerTestUtillities.RunVCom(workingDir, "weazel_text_io.vhd");
			BadgerTestUtillities.RunVCom(workingDir, "*.vhd");
			BadgerTestUtillities.RunVCom(workingDir, "*.vhd");
			BadgerTestUtillities.RunVCom(workingDir, "*.vhd");

			// simulate
			BadgerTestUtillities.RunVSim(workingDir, "work.s", "run 10000ns");
			
			// check that the output files has been written
			Assert.IsTrue(File.Exists(workingDir + "\\" + "monitor_display.txt"));
			Assert.IsTrue(File.Exists(workingDir + "\\" + "stimuli_display.txt"));

			// finally, compare output with reference
			string reference1 = BadgerTestUtillities.GetResourceFile("FullAdder.monitor_display.txt");
			Assert.AreEqual(reference1, BadgerTestUtillities.ReadFile(workingDir + "\\" + "monitor_display.txt"));

			string reference2 = BadgerTestUtillities.GetResourceFile("FullAdder.stimuli_display.txt");
			Assert.AreEqual(reference2, BadgerTestUtillities.ReadFile(workingDir + "\\" + "stimuli_display.txt"));
		}
	}
}
